package com.innjia.base.zima.util;

import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.ZhimaCustomerCertificationCertifyRequest;
import com.alipay.api.request.ZhimaCustomerCertificationInitializeRequest;
import com.alipay.api.request.ZhimaCustomerCertificationQueryRequest;
import com.alipay.api.request.ZhimaCustomerContractInitializeRequest;
import com.alipay.api.response.ZhimaCustomerCertificationCertifyResponse;
import com.alipay.api.response.ZhimaCustomerCertificationInitializeResponse;
import com.alipay.api.response.ZhimaCustomerCertificationQueryResponse;
import com.alipay.api.response.ZhimaCustomerContractInitializeResponse;
import com.innjia.base.utils.Base64Util;

/**
 * 芝麻可信电子合约签约
 * @author fangcheng.huang
 *
 */
public class MayiContractUtil{

	/**
	 * 可信签约初始化
	 * @param params
	 * @return
	 * ZhimaCustomerContractInitializeResponse
	 */
	public static ZhimaCustomerContractInitializeResponse contractInit(Map<String, String> params) {
		AlipayClient alipayClient = new DefaultAlipayClient(params.get("aliPayUrl"), params.get("appId"),
				params.get("privateKey"), params.get("format"), params.get("charset"), params.get("zimaPublicKey"), 
				params.get("signType"));
		ZhimaCustomerContractInitializeRequest request = new ZhimaCustomerContractInitializeRequest();
		//FileUtils.readFileToByteArray(new File(""));
		//Base64.encodeBase64(byte);
		//product_code:w1010100000000002978,contract_name:《XXXXX合同》,contract_file:base64编码
		request.setBizContent(params.get("bizContent"));
		ZhimaCustomerContractInitializeResponse response = null;
		try {
			response = alipayClient.execute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 芝麻认证初始化
	 * @param params
	 * @return
	 * ZhimaCustomerCertificationInitializeResponse
	 */
	public static ZhimaCustomerCertificationInitializeResponse certificationInit(Map<String, String> params) {
		AlipayClient alipayClient = new DefaultAlipayClient(params.get("aliPayUrl"), params.get("appId"),
				params.get("privateKey"), params.get("format"), params.get("charset"), params.get("zimaPublicKey"), 
				params.get("signType"));
		ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();
		request.setBizContent(params.get("bizContent"));
		ZhimaCustomerCertificationInitializeResponse response = null;
		try {
			response = alipayClient.execute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 开始认证
	 * @param params
	 * @return
	 * ZhimaCustomerCertificationCertifyResponse
	 */
	public static ZhimaCustomerCertificationCertifyResponse  certificationCertify(Map<String, String> params) {
		AlipayClient alipayClient = new DefaultAlipayClient(params.get("aliPayUrl"), params.get("appId"),
				params.get("privateKey"), params.get("format"), params.get("charset"), params.get("zimaPublicKey"), 
				params.get("signType"));
		ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();
		request.setBizContent(params.get("bizContent"));
		request.setReturnUrl(params.get("returnUrl"));
		ZhimaCustomerCertificationCertifyResponse response = null;
		try {
			response = alipayClient.pageExecute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}
	
	/**
	 * 芝麻认证查询
	 * @param params
	 * @return
	 * ZhimaCustomerCertificationQueryResponse
	 */
	public static ZhimaCustomerCertificationQueryResponse certificationQuery(Map<String, String> params) {
		AlipayClient alipayClient = new DefaultAlipayClient(params.get("aliPayUrl"), params.get("appId"),
				params.get("privateKey"), params.get("format"), params.get("charset"), params.get("zimaPublicKey"), 
				params.get("signType"));
		ZhimaCustomerCertificationQueryRequest request = new ZhimaCustomerCertificationQueryRequest();
		request.setBizContent(params.get("bizContent"));
		ZhimaCustomerCertificationQueryResponse response = null;
		try {
			response = alipayClient.execute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}
	
	public static void main(String[] args) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("aliPayUrl", "https://openapi.alipay.com/gateway.do");
		params.put("appId", "1001620");
		params.put("privateKey", "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIjfNWUXqCNtlaBFpv4KrBTVQb4btX9Gi+UPuz7AMvwbSXmtIxV2awkz4Rg0Q3PtTkuAUpuW958u3uNxypcsCJD3o4qphgqSMVpe33bJ1bZaeL/h8WT9KNBiIxGsYQco44JbJkjrHTAI3Wt+RNhHyn+nKI9wCaeYPCY7ZipVpmL3AgMBAAECgYBn/BxWx1hIQjMQ5pnuGzGNSk9+HRMQtQoHZqI9FEwn2JtDw9QJtEOxZCa4+svcQQfguIcKCfHqj/NqHMNrglqmmJhKrut+ilUt8rqJCmvRSh8AdG7ZnqzxQYdTrrEEKgO932WqPOI98X2H+2cPzLfIOgxN7FhCDg+lVi4CBdfJ8QJBANosNf2b4f50AgAkstAKX1Hqqsb+k5L5reoF9e6eQufPXIpSrbME3cETyRMMLKX+YfhHpv66fpfg0X4JiHZIpdsCQQCgmmPNWs2nbvHY3Eo3xuYs/KJRIltgYaAd9+CCjpCTbU21afqyGkK7zibHfLADh+m5GOkOZ5IFn5Alhg1NQNgVAkBTX/PeEDVEPWcKUPv4nw4gSvKqi10wHLSGq3J5lwdweQEfZ0s0D5cDEyGTYuKpKNadwBwkWnbIacUFSnVY5phjAkAt4Ix73+F5X77kROFKl52u4if350mU+a5EgUd35AO2qXWWSgTcFZZUkaoQODULfSqtvkjs3Xcf9hm2LlnkZI6VAkEAl8+0+a6Rjz119mxJXy9JWKTbWirhja+ijyndLVEAmxTYjWTuNKbRdTYe59ekRYhmMWkiKzggP+PKjXHCHV6eRQ==");
		params.put("format", "JSON");
		params.put("charset", "utf-8");
		params.put("zimaPublicKey", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCchQZ/IEfYefadapg6L8M1TuWvXIRFTjbURaCOq2Tlw6OKux5L5a5UdPeQx9fTucvsEpRTUztrzEmft+18St2EG0XhunlJ49si/1KRzJX5xvxKe0VL3S50Ru8Kbu4kn+N6vGlhXcPLQM3KsOlvUSoND3zCTgcpzT5Yqvkg5vzAmQIDAQAB");
		params.put("signType", "RSA2");
		Map<String, String> map = new HashMap<String, String>();
		map.put("product_code", "w1010100000000002978");
		map.put("contract_name", "合同");
		map.put("contract_file", Base64Util.encode("heotng".getBytes()));
		params.put("bizContent", "");
		contractInit(params);
	}
}
